subroutine readinp(nunit,fn)
  use param
  use global
  implicit none
  integer,intent(in) :: nunit
  character :: fn*(*)
  character(len=300) :: eatline,trimline
  character(len=30),dimension(10) :: eatword
  integer :: i
  integer :: nstr,asci
  open(unit=nunit,file=fn,status='old')
  do
     read(nunit,'(a)',end=100,err=999) eatline
     nstr=len_trim(eatline)
     if (nstr.eq.0) cycle
     trimline=trim(eatline)
     asci=ichar(trimline(1:1))
     if (asci.eq.33.or.asci.eq.35) cycle
     read(trimline(1:nstr),*,end=101) (eatword(i),i=1,10)
101  continue
     select case(eatword(1))
     case("mode")
        asci=ichar(eatword(2)(1:1))
        if (asci.eq.115.or.asci.eq.83) then
           ! s or T
           rmode=1
        else if (asci.eq.120.or.asci.eq.88) then
           ! x or X
           rmode=2
        else if (asci.eq.112.or.asci.eq.80) then
           ! p or P
           rmode=3
        else
        end if
     case("dise")
        read(eatword(2),*) dise
     case("sube")
        read(eatword(2),*) sube
     case("recoil")
        rfn=trim(eatword(2)) 
     case("xsec")
        if (rmode.eq.2) then
           ! mode=xsec
           read(eatword(2),*) xmode
           if (xmode.eq.1) then
              read(eatword(3),*) nhx
              read(eatword(4),*) nix
           else if (xmode.eq.2) then
              read(eatword(3),*) xmaxe
              read(eatword(4),*) xeh
              read(eatword(5),*) xih
           else
              print '(3x,"Err-Not allowable case")'
              stop
           end if
        else if (rmode.eq.1) then
           ! mode=sub
           read(eatword(2),*) xmode
           if (xmode.eq.1) then
              print '(3x,"Err-xmode=1 is not prepared yet")'
              stop
           else if (xmode.eq.2) then
              xfn=trim(eatword(3))
           else
              print '(3x,"Err-Not allowable case : xmode=2")'
              stop
           end if
        else
           stop
        end if
     case("power")
        if (rmode.eq.3) then
           ! mode=power
           read(eatword(2),*) pmode
           if (pmode.eq.1) then
              read(eatword(3),*) nhp
              read(eatword(4),*) nip
           else if (pmode.eq.2) then
              read(eatword(3),*) pmaxe
              read(eatword(4),*) peh
              read(eatword(5),*) nip
           else
              stop
           end if
        else if (rmode.eq.1) then
           ! mode=sub
           read(eatword(2),*) pmode
           if (pmode.eq.1) then
              print '(3x,"Err-pmode=1 is not prepared yet.")'
              stop
           else if (pmode.eq.2) then
              pfn=trim(eatword(3))
           else
              print '(3x,"Err-Nor allowable case : pmode=2")'
              stop
           end if
        else
           stop
        end if
     case("out")
        nstr=len_trim(eatword(2))
        if (nstr.eq.0) then
           print '("Err-out must be specified as a filename.")'
           stop
        end if
        outfn=trim(eatword(2))
     case("next")
        nstr=len_trim(eatword(2))
        if (nstr.eq.0) then
           print '("Err-h_fnext,h_fcoll must be specified")'
           stop
        end if
        read(eatword(2),*) next_e
        nstr=len_trim(eatword(3))
        if (nstr.eq.0) then
           print '("Err-h_fcoll must be specified")'
           stop
        end if
        read(eatword(3),*) int_n
     case("amass")
        read(eatword(2),*) amass
     case("acharge")
        read(eatword(2),*) acharge
     case("density")
        read(eatword(2),*) ndensity
     case("ngi")
        read(eatword(2),*) ngi
     case default
     end select
  end do
  close(nunit)
999 continue
  print '("Err-readinp")'
  stop
100 continue
end subroutine readinp
